import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# 数据
years = list(range(2010, 2023))  # 年份列表
wheat_prices = [2.12, 2.12, 2.246, 2.341, 2.56, 2.481, 2.35, 2.67, 2.814, 2.832, 2.917, 3.23, 3.25]
corn_prices = [1.812, 1.95, 1.83, 2.31, 2.164, 2.154, 1.84, 2.311, 2.34, 2.412, 2.547, 2.71, 2.61]
soybean_prices = [3.512, 4.23, 3.561, 3.87, 4.614, 5.21, 5.46, 4.27, 4.61, 4.37, 4.534, 4.3, 4.41]
peanut_prices = [5.134, 5.23, 5.56, 5.613, 6.2, 5.94, 5.82, 6.23, 6.56, 6.694, 6.37, 6.812, 6.613]

# 创建图表
fig, ax = plt.subplots()

ax.plot(years, wheat_prices, marker='^', linestyle='-', color='black', label='Wheat')
ax.plot(years, corn_prices, marker='s', linestyle='--', color='black', label='Corn')
ax.plot(years, soybean_prices, marker='D', linestyle='-.', color='black', label='Soybean')
ax.plot(years, peanut_prices, marker='o', linestyle=':', color='black', label='Peanut')

# 添加标题和标签
# ax.set_title('2010-2022年主要农产品价格对比图', fontsize=14, loc='left')
ax.set_xlabel('Year')
ax.set_ylabel('Price (kg/RMB)')

# 显示图例，并放在左下角
plt.legend(loc='upper right', bbox_to_anchor=(1, 0.86), borderpad=1)

# 显示网格
# ax.grid(True)

plt.savefig("售价.png", bbox_inches='tight', dpi=300)  # 设置 dpi 为 300，图片分辨率更高

# 显示图表
plt.tight_layout()  # 调整布局以防止重叠
plt.show()
